<?php

namespace backend\controllers;

use Yii;

use backend\models\form\LogForm;
use yii\web\Controller;

use yii\filters\VerbFilter;

/**
 * 错误日志记录
 */
class LogController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * 显示日志列表
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new LogForm();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $attributeLabels = $searchModel->attributeLabels();
        $logs = $dataProvider->getModels();
        $logs = $this->formatLog($logs);//格式化数据
        $page = $dataProvider->getPagination();

        return $this->render('index', [
            'searchModel' => $searchModel,
            'logs' => $logs,
            'page' => $page,
            'attributeLabels' => $attributeLabels,
        ]);
    }

    /**
     * 格式化日志
     * @param $logs
     * @return mixed
     */
    public function formatLog($logs)
    {
        $logArray = ['1'=>'error','4'=>'info'];
        foreach ($logs as $k => $log) {
            $logs[$k]->log_time = date('Y-m-d H:i:s',$log->log_time);
            $log->level = $logArray[$log->level];
            $log->message = str_replace("\n", 'linshibianliang', $log->message);
            $log->message = htmlspecialchars($log->message);
            $logs[$k]->message = str_replace("linshibianliang","<br/>", $log->message);
        }
        return $logs;
    }

}
